package com.alumni_circle.controller.home;

import cn.hutool.core.util.StrUtil;
import com.alumni_circle.core.config.SysLog;
import com.alumni_circle.core.entity.BaseId;
import com.alumni_circle.core.entity.PageBase;
import com.alumni_circle.core.entity.ResponseListModel;
import com.alumni_circle.core.entity.ResponseModel;
import com.alumni_circle.core.utils.ModelUtils;
import com.alumni_circle.core.utils.PictureBase64;
import com.alumni_circle.core.utils.SecurityUtils;
import com.alumni_circle.core.web.BaseController;
import com.alumni_circle.entity.home.LostFound;
import com.alumni_circle.entity.home.VO.LostFoundInsertVO;
import com.alumni_circle.entity.home.VO.LostFoundViewVO;
import com.alumni_circle.entity.home.issue.UserLost;
import com.alumni_circle.entity.me.User;
import com.alumni_circle.service.home.ILostFoundService;
import com.alumni_circle.service.home.issue.IUserLostService;
import com.alumni_circle.utils.MsgPermissions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

import static com.alumni_circle.core.constant.ResponseCode.PERMISSIONS;

/**
 * @author 龙小虬
 * @since 2020-07-06 22:58
 */
@Api(tags = "丢失管理")
@Controller
@RequestMapping("/lostFound")
public class LostFoundController extends BaseController<LostFound> {

    @Resource
    private ILostFoundService lostFoundService;

    @Resource
    private IUserLostService userLostService;

    @Resource
    MsgPermissions msgPermissions;

    /**
     * time,content,place,picture,phone,goods,token
     * 增加丢失信息
     * @param lostFoundInsertVO
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addLostFound", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "新增丢失信息", notes = "增加丢失信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="增",accessInfo="新增丢失信息",url="/lostFound/addLostFound")
    public ResponseModel<LostFoundInsertVO> addLostFound(@ApiParam(required = true, name = "lostFoundInsertVO")
                                         @RequestBody LostFoundInsertVO lostFoundInsertVO) {
        String path="";
        if(!StrUtil.hasEmpty(lostFoundInsertVO.getPicture())&& lostFoundInsertVO.getPicture().contains("{-temporary}")) {
            path = lostFoundInsertVO.getPicture();
            lostFoundInsertVO.setPicture("");
        }
        LostFound lostFound = ModelUtils.transform(lostFoundInsertVO,LostFound.class);
        User user = SecurityUtils.getUser();
        lostFound.setCreateBy(user.getId());
        lostFound.setCreateName(user.getName());
        lostFoundService.save(lostFound);
        super.picturePath(1,lostFound.getId(),path);
        UserLost userLost = new UserLost();
        userLost.setLostId(lostFound.getId());
        userLost.setUserId(user.getId());
        userLostService.save(userLost);
        return new ResponseModel<>(lostFoundInsertVO);
    }

    /**
     * id，time,content,place,phone,goods,token
     * @param lostFoundInsertVO
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/updateLostFound", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "更新丢失信息", notes = "更新丢失信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="改",accessInfo="更新丢失信息",url="/lostFound/updateLostFound")
    public ResponseModel updateLostFound(@ApiParam(required = true, name = "lostFoundInsertVO")
                                            @RequestBody LostFoundInsertVO lostFoundInsertVO) {

        if(msgPermissions.updateDelete(lostFoundInsertVO.getId(), "lost")){
            return new ResponseModel<>(PERMISSIONS.getCode(),PERMISSIONS.getMessage());
        }
        LostFound lostFound = ModelUtils.transform(lostFoundInsertVO,LostFound.class);
        lostFoundService.updateById(lostFound);
        return new ResponseModel<>("success");
    }

    /**
     * id
     * 通过文章id获取此文章信息
     *
     * @param baseId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/selectLostFound", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "查询丢失信息", notes = "查询丢失信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="查",accessInfo="查询丢失信息",url="/lostFound/selectLostFound")
    public ResponseModel<Object> selectLostFound(@ApiParam(required = true, name = "baseId")
                                            @RequestBody BaseId baseId) {

        LostFound lostFound = lostFoundService.getById(baseId.getId());
        return new ResponseModel<>(ModelUtils.transform(lostFound, LostFoundInsertVO.class));
    }

    /**
     * 搜索丢失信息
     * @param pageBase
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/searchLostFound", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "查询丢失信息", notes = "查询丢失信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="查",accessInfo="查询丢失信息",url="/lostFound/searchLostFound")
    public ResponseListModel<LostFoundViewVO> searchLostFound(@ApiParam(required = true, name = "pageBase")
                                         @RequestBody PageBase pageBase) {
        PageHelper.startPage(pageBase.getPageNum(),pageBase.getPageSize());
        List<LostFoundViewVO>list = lostFoundService.searchListLostFound(pageBase.getContent());
        for (LostFoundViewVO lostFoundViewVO : list) {
            //取消头像下载，使用微信提供的链接
//            lostFoundViewVO.setIcon(PictureBase64.imageStr(lostFoundViewVO.getIcon()));
            if (!StrUtil.hasEmpty(lostFoundViewVO.getPicture())) {
                lostFoundViewVO.setPicture(PictureBase64.thumbnailList(lostFoundViewVO.getPicture()));
            }
        }
        return new ResponseListModel<>(list);

    }

    /**
     * 删除信息
     * @param baseId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/deleteLostFound", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "删除丢失信息", notes = "删除丢失信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="删",accessInfo="删除丢失信息",url="/lostFound/deleteLostFound")
    public ResponseModel<java.io.Serializable> deleteLostFound(@ApiParam(required = true, name = "baseId")
                                            @RequestBody BaseId baseId) {

        if(msgPermissions.updateDelete(baseId.getId(), "lost")){
            return new ResponseModel<>(PERMISSIONS.getCode(),PERMISSIONS.getMessage());
        }
        lostFoundService.removeById(baseId.getId());
        QueryWrapper<UserLost> queryWrapper = new QueryWrapper<UserLost>();
        queryWrapper.eq("lost_id",baseId.getId());
        userLostService.remove(queryWrapper);
        super.picturePathDelete(baseId.getId());
        return new ResponseModel<>();
    }



    /**
     * 查询所有丢失信息
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/selectListLostFound", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "查询丢失信息", notes = "丢失信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="查",accessInfo="查询丢失信息",url="/lostFound/selectListLostFound")
    public ResponseListModel<LostFoundViewVO> selectListLostFound(@ApiParam(required = true, name = "pageBase")
                                                                  @RequestBody PageBase pageBase) {
        PageHelper.startPage(pageBase.getPageNum(),pageBase.getPageSize());
        List<LostFoundViewVO>list = lostFoundService.selectListLostFound();
        PageInfo<LostFoundViewVO>pageInfo = new PageInfo<>(list);
        for (LostFoundViewVO lostFoundViewVO : list) {
            //取消头像下载，使用微信提供的链接
//            lostFoundViewVO.setIcon(PictureBase64.imageStr(lostFoundViewVO.getIcon()));
            if (!StrUtil.hasEmpty(lostFoundViewVO.getPicture())) {
                lostFoundViewVO.setPicture(PictureBase64.thumbnailList(lostFoundViewVO.getPicture()));
            }

        }
        return new ResponseListModel<>(list,String.valueOf(pageInfo.getTotal()));
    }

    /**
     * token
     * 通过token获得用户id，查询用户发布的所有丢失信息
     * @param pageBase
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/selectMyListLostFound", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "搜索丢失信息", notes = "搜索丢失信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="查",accessInfo="搜索丢失信息",url="/lostFound/selectMyListLostFound")
    public ResponseListModel<LostFoundViewVO> selectMyListLostFound(@ApiParam(required = true, name = "pageBase")
                                                                    @RequestBody PageBase pageBase) {

        PageHelper.startPage(pageBase.getPageNum(),pageBase.getPageSize());
        List<LostFoundViewVO>list = lostFoundService.selectMyListLostFound(SecurityUtils.getId());
        for (LostFoundViewVO lostFoundViewVO : list) {
            //取消头像下载，使用微信提供的链接
//            lostFoundViewVO.setIcon(PictureBase64.imageStr(lostFoundViewVO.getIcon()));
            if (!StrUtil.hasEmpty(lostFoundViewVO.getPicture())) {
                lostFoundViewVO.setPicture(PictureBase64.thumbnailList(lostFoundViewVO.getPicture()));
            }

        }
        return new ResponseListModel<>(list);
    }


    /**
     * token,content
     * 根据token拿取用户id，关键字搜索自己发布的信息
     * @param pageBase
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/searchMyListLostFound", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "搜索丢失信息", notes = "搜索丢失信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="查",accessInfo="搜索丢失信息",url="/lostFound/searchMyListLostFound")
    public ResponseListModel<LostFoundViewVO> searchMyListLostFound(@ApiParam(required = true, name = "pageBase")
                                                                    @RequestBody PageBase pageBase) {
        PageHelper.startPage(pageBase.getPageNum(),pageBase.getPageSize());
        List<LostFoundViewVO>list = lostFoundService.searchMyListLostFound(SecurityUtils.getId(),pageBase.getContent());
        for (LostFoundViewVO lostFoundViewVO : list) {
            //取消头像下载，使用微信提供的链接
//            lostFoundViewVO.setIcon(PictureBase64.imageStr(lostFoundViewVO.getIcon()));
            if (!StrUtil.hasEmpty(lostFoundViewVO.getPicture())) {
                lostFoundViewVO.setPicture(PictureBase64.thumbnailList(lostFoundViewVO.getPicture()));
            }

        }
        return new ResponseListModel<>(list);
    }



    @ResponseBody
    @RequestMapping(value = "/getAllLostFound", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "查询符合条件丢失信息", notes = "查询符合条件丢失信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="查",accessInfo="查询符合条件丢失信息",url="/lostFound/getAllLostFound")
    public ResponseListModel<LostFoundViewVO> getAllLostFound(@ApiParam(required = true, name = "baseId")
                                             @RequestBody BaseId baseId) {
        return new ResponseListModel<>(lostFoundService.selectListLostFound());
    }

    @ResponseBody
    @RequestMapping(value = "/selectLostFoundWrapper", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "搜索丢失信息", notes = "搜索丢失信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="查",accessInfo="搜索丢失信息",url="/lostFound/selectLostFoundWrapper")
    public ResponseListModel<LostFound> selectLostFoundWrapper(@ApiParam(required = true, name = "lostFound")
                                                               @RequestBody LostFound lostFound) {
        List<LostFound>list =  lostFoundService.list(new QueryWrapper<LostFound>().and(warpper -> warpper.like("goods",lostFound.getGoods()).or().like("place",lostFound.getPlace())));
        for (LostFound found : list) {
            found.setPicture(PictureBase64.thumbnailList(found.getPicture()));
        }
        return new ResponseListModel<>(list);

    }

    @ResponseBody
    @RequestMapping(value = "/selectMyLostFoundWrapper", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "搜索丢失信息", notes = "搜索丢失信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="查",accessInfo="搜索丢失信息",url="/lostFound/selectMyLostFoundWrapper")
    public ResponseListModel<LostFound> selectMyLostFoundWrapper(@ApiParam(required = true, name = "LostFound")
                                                                 @RequestBody LostFound lostFound) {
        return new ResponseListModel<>(lostFoundService.list(new QueryWrapper<LostFound>().and(warpper -> warpper.like("goods",lostFound.getGoods()))));

    }

}
